/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyts.infodb.config.ModuleNameEnum;
import com.hyts.infodb.config.SystemConfiguration;
import com.hyts.infodb.config.SystemConfiguration.MessageConfiguration;
import com.hyts.infodb.user.bean.UserBO;
import com.hyts.infodb.user.bean.UserDTO;
import com.hyts.infodb.user.bean.UserVO;
import com.hyts.infodb.user.service.UserService;

/** 
 * @author LiBo/Alex
 * @version V1.0 
 */
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService service;

    /**
     * <b>查询基本信息</b>
     * @param id
     * @return
     * @exception
     */
    @RequestMapping("/load/{id}")
    public UserDTO loadUserInfo(@PathVariable("id") Long id) {
        UserDTO userDTO = new UserDTO();
        UserBO bo = new UserBO();
        bo.setId(id);
        UserVO vo = service.loadUser(bo);
        if(vo == null || vo.getContent() == null) {
            return (UserDTO) userDTO.failure(MessageConfiguration.LOAD,ModuleNameEnum.USER_MODULE.getModuleName());
        }else {
            return (UserDTO) userDTO.success(MessageConfiguration.LOAD,ModuleNameEnum.USER_MODULE.getModuleName(),vo.getContent());
        }
    }
    
    /**
     * <b>保存基本信息</b>
     * @return
     * @exception
     */
    @RequestMapping("/save")
    public UserDTO saveUserInfo(@RequestBody UserBO user) {
        UserDTO userDTO = new UserDTO();
        boolean result = service.saveUser(user);
        if(result) {
            return (UserDTO) userDTO.success(MessageConfiguration.SAVE,ModuleNameEnum.USER_MODULE.getModuleName());
        }else {
            return (UserDTO) userDTO.failure(MessageConfiguration.SAVE,ModuleNameEnum.USER_MODULE.getModuleName());
        }
    }
    
    /**
     * <b>删除基本信息</b>
     * @param id
     * @return
     * @exception
     */
    @RequestMapping("/delete/{id}")
    public UserDTO deleteUserInfo(@PathVariable("id") Long id) {
        UserDTO userDTO = new UserDTO();
        UserBO bo = new UserBO();
        bo.setId(id);
        boolean result = service.deleteUser(bo);
        if(result) {
            return (UserDTO) userDTO.success(MessageConfiguration.REMOVE,ModuleNameEnum.USER_MODULE.getModuleName());
        }else {
            return (UserDTO) userDTO.failure(MessageConfiguration.REMOVE,ModuleNameEnum.USER_MODULE.getModuleName());
        }
    }
    
    /**  
     * @return
     * @exception
     */ 
    @RequestMapping("/find")
    public UserDTO findUserList(@RequestBody UserBO user) {
        UserVO vo = service.findUserList(user);
        UserDTO userDTO = new UserDTO();
        if(vo == null) {
            return (UserDTO) userDTO.failure(MessageConfiguration.QUERY,ModuleNameEnum.USER_MODULE.getModuleName());
        }else {
            return (UserDTO) userDTO.success(MessageConfiguration.QUERY,ModuleNameEnum.USER_MODULE.getModuleName(),vo.getContents());
        }
    }

    @RequestMapping("/get/{userName}")
    public JSONObject getUser(@PathVariable  String userName) {
        UserVO vo = service.getUser(userName);
        com.hyts.infodb.shiro.bean.UserVO userVO2 = new com.hyts.infodb.shiro.bean.UserVO();
        BeanUtils.copyProperties(vo,userVO2);
        return JSONObject.parseObject(JSON.toJSONString(userVO2));
    }
    
    @RequestMapping("/login")
    public UserDTO login(@RequestBody UserBO user) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken();
        token.setUsername(user.getUserName());
        token.setPassword(user.getPassword().toCharArray());
        UserDTO userDTO = new UserDTO();
        try {
            subject.login(token);
            userDTO.setCode(SystemConfiguration.ResultConfiguration.SUCCESS.getCode());
            System.out.println(SecurityUtils.getSubject().isAuthenticated());
        } catch (Exception e) {
        	userDTO.setCode(SystemConfiguration.ResultConfiguration.FAILURE.getCode());
        	if(e instanceof IncorrectCredentialsException) {
                userDTO.setMessage("用户名或者密码错误!");
        	}else
        		userDTO.setMessage(e.getMessage());
        }
        return userDTO;
    }
    
}
